/* 
store对象
*/
import {createStore, applyMiddleware, combineReducers} from 'redux'
import thunk from 'redux-thunk'
import {composeWithDevTools} from 'redux-devtools-extension'
import counterReducer from './counter'
import userSearchReducer from './userSearch'
import { Store } from 'antd/lib/form/interface'

/* 
当应用中有多个模块的不同数据需要管理时  => 有多个reducer
解决: 使用redux提供combineReducers来合并多个reducer来管理所有模块的状态数据
    返回一个管理所有state的根/总reducer
    function (state, action) {} // state就是总的state: 包含所有模块的state
总的state的结构
    { 
      counter:  {
          count: 3
        },

      userSearch: {
          firstView: true,
          loading: false,
          errorMsg: '',
          users: []
        }
    }
*/
const rootReducer = combineReducers({
  counter: counterReducer,
  userSearch: userSearchReducer
})

// 创建store, 指定reducer函数, 应用上异步插件
const store = createStore(
  rootReducer, 
  composeWithDevTools(applyMiddleware(thunk))
)

// 向外默认暴露store
export default store

// 向外暴露根state的类型
// 写死了
/* type RootState = {  
  counter: {
    count: number
  },
  userSearch: {
    firstView: true,
    loading: false,
    errorMsg: '',
    users: []
  }
} */

// const state = store.getState()
// state.userSearch.users

/* 
动态得到 store.getState 函数返回值的类型
*/
// 得到的是getState函数的类型
type GetState = typeof store.getState
// 得到指定函数类型的返回值类型
export type RootState = ReturnType<GetState>

// export type RootState2 = ReturnType<typeof store.getState>